home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / c_life.zip / LIFE.C next >
C/C++ Source or Header  |  1993-08-30  |  3KB  |  127 lines

  1. /* LIFE.C -- C. Reace, May 1993 */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <conio.h>
  7. #include <time.h>
  8.  
  9. int life[25][42];
  10. int cur_row, cur_col;
  11. int life_color[3] = {BLUE, YELLOW, WHITE};
  12. char life_char[3] = {' ', '*', '*'};
  13. int generation = 1, population = 0;
  14. char done = '';
  15.  
  16. main(void)
  17. {
  18.     do {
  19.        /* Initialize all cells to 0 (no life) */
  20.        randomize();
  21.        for (cur_row = 0; cur_row < 25; cur_row ++) {
  22.            for (cur_col = 0; cur_col < 42; cur_col ++) {
  23.                life[cur_row][cur_col] = 0;
  24.            }
  25.        }
  26.        setcbrk(1);    /* Test for break each system call */
  27.        textmode(C40);
  28.        textbackground(life_color[0]);
  29.        clrscr();
  30.  
  31.        /* Randomly set some central cells to '1' (new life) */
  32.        for (cur_row = 12; cur_row < 15; cur_row ++) {
  33.            for (cur_col = 19; cur_col < 23; cur_col ++) {
  34.                life[cur_row][cur_col] = random(2);
  35.                if (life[cur_row][cur_col] == 1) {
  36.                    population = population + 1;
  37.                }
  38.            }
  39.        }
  40.        _setcursortype(_NOCURSOR);
  41.        show_life();
  42.        for (generation = 2;population > 0 && generation < 200 && !kbhit();
  43.             generation ++) {
  44.            calc_life();
  45.            show_life();
  46.        }
  47.     }
  48.     while (!kbhit());
  49.     textmode(C80);
  50.     clrscr();
  51.     return;
  52. }
  53.  
  54. calc_life(void)
  55. {
  56.     int new_life[25][42];
  57.     int n_row, n_col;
  58.     int neighbors;
  59.  
  60.     population = 0;
  61.     for (cur_row = 0; cur_row < 25; cur_row ++) {
  62.         for (cur_col = 0; cur_col < 42; cur_col ++) {
  63.             new_life[cur_row][cur_col] = life[cur_row][cur_col];
  64.         }
  65.     }
  66.     for (cur_row = 1; cur_row < 24; cur_row ++) {
  67.         for (cur_col = 1; cur_col < 41; cur_col ++) {
  68.             neighbors = 0;
  69.             for (n_col = cur_col - 1; n_col < cur_col + 2; n_col ++) {
  70.                 if (life[cur_row - 1][n_col] > 0) {
  71.                     neighbors = neighbors + 1;
  72.                 }
  73.             }
  74.             for (n_col = cur_col -1; n_col < cur_col +2; n_col = n_col + 2) {
  75.                 if (life[cur_row][n_col] > 0) {
  76.                     neighbors = neighbors + 1;
  77.                 }
  78.             }
  79.             for (n_col = cur_col - 1; n_col < cur_col + 2; n_col ++) {
  80.                 if (life[cur_row + 1][n_col] > 0) {
  81.                     neighbors = neighbors + 1;
  82.                 }
  83.             }
  84.             if (life[cur_row][cur_col] == 0) {
  85.                 if (neighbors == 3) {
  86.                     new_life[cur_row][cur_col] = 1;
  87.                     population = population + 1;
  88.                 }
  89.             }
  90.             else {
  91.                 if (neighbors < 2 || neighbors > 4) {
  92.                     new_life[cur_row][cur_col] = 0;
  93.                 }
  94.                 else {
  95.                     new_life[cur_row][cur_col] = 2;
  96.                     population = population + 1;
  97.                 }
  98.             }
  99.         }
  100.     }
  101.     for (cur_row = 0; cur_row < 25; cur_row ++) {
  102.         for (cur_col = 0; cur_col < 42; cur_col ++) {
  103.             life[cur_row][cur_col] = new_life[cur_row][cur_col];
  104.         }
  105.     }
  106.     return;
  107. }
  108.  
  109. show_life(void)
  110. {
  111.     gotoxy(1,1);
  112.     textcolor(WHITE);
  113.     textbackground(GREEN);
  114.     cprintf("Generation: %i  %s %i",generation,"Population: ",population);
  115.     clreol();
  116.     gotoxy(1,2);
  117.     textbackground(BLUE);
  118.     for (cur_row = 1; cur_row < 24; cur_row ++) {
  119.         gotoxy(1, cur_row + 1);
  120.         for (cur_col = 1; cur_col < 41; cur_col ++) {
  121.             textcolor(life_color[life[cur_row][cur_col]]);
  122.             putch(life_char[life[cur_row][cur_col]]);
  123.         }
  124.     }
  125.     return;
  126. }
  127.